﻿using Gx.Common;
using Gx.Common.Core;
using Gx.Dal;
using Gx.Dal.Manage;
using Gx.Dal.Shared;
using Gx.Model.Entity;
using Gx.Model.ViewModel;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Gx.Bll.Manage
{
    public class LKCustomerBll
    {
        /// <summary>
        /// 新增客户
        /// </summary>
        /// <param name="customer"></param>
        /// <returns></returns>
        public bool AddCustomer(LKCustomer customer)
        {
            LKCustomerDal dal = new LKCustomerDal();
            customer.CustomerCode = GetCusomerCode();

            customer.CreateTime = DateTime.Now;
            customer.CreateUserId = LoginHelper.GetUserId();
            customer.CreateUser = LoginHelper.UserName();
            customer.CompanyId = UserContext.CompanyId;
            return dal.Insert(customer);//dal.AddCustomer(customer);
        }

        private string GetCusomerCode()
        {
            LKCustomerDal dal = new LKCustomerDal();
            var maxCode = dal.GetMaxCustomerCode();
            if (string.IsNullOrWhiteSpace(maxCode))
            {
                return "1000";
            }
            return (int.Parse(maxCode) + 1).ToString();
        }
        public bool EditCustomer(LKCustomer customer)
        {
            LKCustomerDal dal = new LKCustomerDal();
            var cus = dal.Get(customer.Id);
            if (cus == null)
            {
                return false;
            }
            customer.CreateTime = cus.CreateTime;
            customer.CreateUserId = cus.CreateUserId;
            customer.CreateUser = cus.CreateUser;
            customer.ModifyUserId = LoginHelper.GetUserId();
            customer.ModifyUser = LoginHelper.UserName();
            customer.ModifyTime = DateTime.Now;
            customer.CompanyId = UserContext.CompanyId;
            return dal.Update(customer);
        }

        //删除客户
        public bool DeleteCustomer(int id)
        {
            LKCustomerDal dal = new LKCustomerDal();
            return dal.DeleteById(id);
        }

        /// <summary>
        /// 客户列表
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public PagedList<LKCustomer> GetCustomerPageList(CustomerInput input)
        {
            LKCustomerDal dal = new LKCustomerDal();
            return dal.GetCustomerList(input);
        }
    }
}
